package com.nuo.common.core.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nuo.common.core.page.PageResult;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @author LGC
 * @date 2022/1/26 11:36
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -1455858674704802637L;

    @ApiModelProperty("状态码")
    private String code;

    @ApiModelProperty("提示信息")
    private String msg;

    @ApiModelProperty("具体对象信息")
    private T data;


    public static <T> Result<T> success() {
        return Result.success(null);
    }


    public static <T> Result<T> success(T t) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(t);
        return result;
    }

    public static boolean isSuccess(Result<?> result) {
        return result != null && ResultCode.SUCCESS.getCode().equals(result.getCode());
    }

    public static <T> Result<T> failed() {
        return failed(ResultCode.ERROR);
    }

    public static <T> Result<T> failed(IResultCode iResultCode) {
        return new Result<>(iResultCode.getCode(), iResultCode.getMsg(), null);
    }

    public static <T> Result<T> failed(IResultCode iResultCode, String message) {
        return new Result<>(iResultCode.getCode(), iResultCode.getMsg() + message, null);
    }

    public static <T> Result<T> failed(String message) {
        return new Result<T>(ResultCode.ERROR.getCode(), message, null);
    }


    public static <T> Result<T> judge(boolean status) {
        if (status) {
            return success();
        } else {
            return failed();
        }
    }

    /**
     * 分页对象成功返回
     *
     * @param pageResult 分页结果对象
     * @return 响应对象
     */
    public static <T> Result<PageResult<T>> page(PageResult<T> pageResult) {
        Result<PageResult<T>> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(pageResult);
        return result;
    }

    /**
     * @param data      数据
     * @param pageNum   当前页
     * @param pageSize  当前页条数
     * @param pageCount 总页数
     * @param total     总条数
     * @return 响应对象
     **/
    public Result<PageResult<T>> page(List<T> data, long pageNum, long pageSize, long pageCount, long total) {
        Result<PageResult<T>> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(PageResult.getPageResult(data, pageNum, pageSize, pageCount, total));
        return result;
    }

    /**
     * @param data     集合
     * @param pageNum  当前页
     * @param pageSize 每页条数
     * @param total    总条数
     * @return 列表对象
     */
    public Result<PageResult<T>> page(List<T> data, Integer pageNum, Integer pageSize, Integer total) {
        Result<PageResult<T>> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(PageResult.getPageResult(data, pageNum, pageSize, total));
        return result;
    }

}
